//
//  KATGifTableHeader.m
//  KATFramework
//
//  Created by Kantice on 16/7/25.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATGifTableHeader.h"

@implementation KATGifTableHeader

#pragma -mark 类方法

//获取实例
+ (instancetype)tableHeaderWithHeight:(float)height gif:(KATGifImage *)gif andScrollView:(UIScrollView *)scrollView
{
    if(!scrollView || !gif)
    {
        return nil;
    }
    
    KATGifTableHeader *header=[[[self alloc] init] autorelease];
    
    [header initWithHeight:height andScrollView:scrollView];
    header.stateDelegate=header;
    header.isReleaseToLoad=NO;
    
    header.hasTite=NO;
    header.gif=gif;
    
    header.infoPulling=@"pull to load!";
    header.infoLoading=@"loading...";
    
    return header;
}


#pragma -mark 对象方法


//初始化
- (void)initHeader
{
    //标题
    self.title=[[[UILabel alloc] init] autorelease];
    _title.frame=CGRectMake(self.bounds.size.height*1.0, self.bounds.size.height*0.2, self.bounds.size.width-self.bounds.size.height*2.0, self.bounds.size.height*0.5);
    _title.text=_infoPulling;
    _title.textColor=KCOLOR_DARK;
    _title.textAlignment=NSTextAlignmentCenter;
    _title.numberOfLines=0;
    _title.font=[UIFont systemFontOfSize:self.bounds.size.height*0.28];
    _title.backgroundColor=[UIColor clearColor];
    [self addSubview:_title];
    
    if(!_hasTite)
    {
        _title.hidden=YES;
    }
    
    //GIF
    self.gif.center=CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.5);
    [self addSubview:_gif];
    
    //提示
    self.tips=[KATToast toast];
    _tips.position=TOAST_POSITION_BOTTOM;
    _tips.duration=TOAST_DURATION_SHORT;
    _tips.canTap=YES;
    
}


//加载完成
- (void)finishWithMessage:(NSString *)message
{
    _title.text=_infoPulling;
    
    [_gif clearAnimations];
    [_gif restoreSprite];
    
    if(message)//显示toast
    {
        _tips.text=message;
        [_tips show];
    }
    
    [self finishLoad];
}



#pragma -mark 回调方法

//下拉
- (void)headerPullWithProgress:(float)progress
{
    _title.text=_infoPulling;
}


//下拉完成
- (void)headerFinishPull
{
    [self headerPullWithProgress:1.0f];
}


//开始加载
- (void)headerStartLoad
{
    _title.text=_infoLoading;
    
    [_gif play];
}


//加载完成
- (void)headerFinishLoad
{
    
}


//加载完成后恢复
- (void)headerRestoreAfterLoad
{
    _title.text=_infoPulling;
    
    [_gif clearAnimations];
    [_gif restoreSprite];
}


//释放内存
- (void)dealloc
{
    [_gif clearAnimations];
    
    [_title release];
    [_gif release];
    [_infoPulling release];
    [_infoLoading release];
    [_tips release];
    
    [super dealloc];
}


@end
